package org.ufba.buscador.parser.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Inflector {

	private static Map<String, String> irregular = new HashMap<String, String>();
	private static Map<String, String> rules = new LinkedHashMap<String, String>();
	
	private static String uniflected = "?:arco-iris|atlas|lapis|onibus|pires|virus|.*x";
	
	static
	{
		irregular.put("abdomens"	, "abdomen");
		irregular.put("alemao"	, "alemaes"); 
		irregular.put("artesa"	, "artesaos"); 
		irregular.put("as"		, "ases"); 
		irregular.put("bencao"	, "bencaos"); 
		irregular.put("cao"		, "caes"); 
		irregular.put("capelao"	, "capelaes"); 
		irregular.put("capitao"	, "capitaes"); 
		irregular.put("chao"		, "chaos"); 
		irregular.put("charlatao" , "charlataes"); 
		irregular.put("cidadao"	, "cidadaos"); 
		irregular.put("consul"	, "consules"); 
		irregular.put("cristao"	, "cristaos"); 
		irregular.put("dificil"	, "dificeis"); 
		irregular.put("email"		, "emails"); 
		irregular.put("escrivao"	, "escrivaes"); 
		irregular.put("fossel"	, "fosseis"); 
		irregular.put("germens"	, "germen"); 
		irregular.put("grao"		, "graos"); 
		irregular.put("hifens"	, "hifen"); 
		irregular.put("irmao"		, "irmaos"); 
		irregular.put("liquens"	, "liquen"); 
		irregular.put("mal"		, "males"); 
		irregular.put("mao"		, "maos"); 
		irregular.put("orfao"		, "orfaos"); 
		irregular.put("pais"		, "paises"); 
		irregular.put("pai"		, "pais"); 
		irregular.put("pao"		, "paes");
		irregular.put("perfil"	, "perfis"); 
		irregular.put("projetil"	, "projeteis"); 
		irregular.put("reptil"	, "repteis"); 
		irregular.put("sacristao" , "sacristaes"); 
		irregular.put("sotao"		, "sotaos"); 
		irregular.put("tabeliao"  , "tabeliaes");
		
		rules.put("^(.*)(oes|aes|aos)$", "$1ao"); 
		rules.put("^(.*)(a|e|o|u)is$","$1$2l"); 
		rules.put("^(.*)e?is$","$1il");
		rules.put("^(.*)(r|s|z)es$","$1$2"); 
		rules.put("^(.*)ns$","$1m");
		rules.put("^(.*)s$","$1"); 
		
	}
	
	public static String singularizeWords(String expression) {
		
		String[] words = expression.split(" ");
		for (int i = 0; i < words.length; i++)
		{
			words[i] = singularizeWord(words[i]);
		}
		
		return implode(words, " ");
	}
	
	public static String singularizeWord(String word)
	{
		if (irregular.containsKey(word))
			return irregular.get(word);
		
		String re = "^(?i)(" + uniflected + ")$";
		if (word.matches(re))
			return word;
		
		for (String key : rules.keySet())
		{
			//System.out.println(key);
			if (word.matches(key))
			{
				//System.out.println("  entrou " + rules.get(key));
				return word.replaceAll(key, rules.get(key));
			}
		}
		
		return word;
	}
	
	public static String implode(String[] ary, String delim) 
	{
	    String out = "";
	    for(int i=0; i<ary.length; i++) {
	        if(i!=0) { out += delim; }
	        out += ary[i];
	    }
	    return out;
	}
}
